package org.deschutter.scans;

import org.deschutter.constants.Config;
import org.deschutter.exceptions.AddDevelopmentException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: Berten De Schutter
 * Date: 01-Jul-2009
 * Time: 11:27:21
 */
public class Development extends Scan {
    private BigDecimal lightFactory, mediumFactory, heavyFactory, amplifier, distorter, metalRefinery, crystalRefinery, eoniumRefinery, researchLab, financeCentre, securityCentre;
    private BigDecimal spaceTravel, infraStructure, hulls, waves, coreExtraction, covertOps, asteroidMining;

    public Development(String ID, BigDecimal tick, String x, String y, String z, String scanpage, BigDecimal userID, Session session) {
        super(ID, Config.DEVELOPMENT_SCAN, tick, x, y, z, scanpage, userID, session);
    }

    protected String parseScan() {


        Pattern compiledRegex;
        Matcher regexMatcher;

        // Get Constructions
        compiledRegex = Pattern.compile("<tr><td[^>]*>Light\\s+Factory</td><td[^>]*>(\\d*)</td></tr>\\s*" +
                "<tr><td[^>]*>Medium\\s+Factory</td><td[^>]*>(\\d*)</td></tr>\\s*" +
                "<tr><td[^>]*>Heavy\\s+Factory</td><td[^>]*>(\\d*)</td></tr>\\s*" +
                "<tr><td[^>]*>Wave\\s+Amplifier</td><td[^>]*>(\\d*)</td></tr>\\s*" +
                "<tr><td[^>]*>Wave\\s+Distorter</td><td[^>]*>(\\d*)</td></tr>\\s*" +
                "<tr><td[^>]*>Metal\\s+Refinery</td><td[^>]*>(\\d*)</td></tr>\\s*" +
                "<tr><td[^>]*>Crystal\\s+Refinery</td><td[^>]*>(\\d*)</td></tr>\\s*" +
                "<tr><td[^>]*>Eonium\\s+Refinery</td><td[^>]*>(\\d*)</td></tr>\\s*" +
                "<tr><td[^>]*>Research\\s+Laboratory</td><td[^>]*>(\\d*)</td></tr>\\s*" +
                "<tr><td[^>]*>Finance\\s+Centre</td><td[^>]*>(\\d*)</td></tr>\\s*" +
                "<tr><td[^>]*>Security\\s+Centre</td><td[^>]*>(\\d*)</td></tr>");
        regexMatcher = compiledRegex.matcher(getScanpage().replace(",", ""));
        regexMatcher.find();

        lightFactory = new BigDecimal(regexMatcher.group(1));
        mediumFactory = new BigDecimal(regexMatcher.group(2));
        heavyFactory = new BigDecimal(regexMatcher.group(3));
        amplifier = new BigDecimal(regexMatcher.group(4));
        distorter = new BigDecimal(regexMatcher.group(5));
        metalRefinery = new BigDecimal(regexMatcher.group(6));
        crystalRefinery = new BigDecimal(regexMatcher.group(7));
        eoniumRefinery = new BigDecimal(regexMatcher.group(8));
        researchLab = new BigDecimal(regexMatcher.group(9));
        financeCentre = new BigDecimal(regexMatcher.group(10));
        securityCentre = new BigDecimal(regexMatcher.group(11));

        // Get Research Levels
        compiledRegex = Pattern.compile("<tr><td[^>]*>Space\\s+Travel</td><td[^>]*>(\\d+)</td></tr>\\s*" +
                "<tr><td[^>]*>Infrastructure</td><td[^>]*>(\\d+)</td></tr>\\s*" +
                "<tr><td[^>]*>Hulls</td><td[^>]*>(\\d+)</td></tr>\\s*" +
                "<tr><td[^>]*>Waves</td><td[^>]*>(\\d+)</td></tr>\\s*" +
                "<tr><td[^>]*>Core\\s+Extraction</td><td[^>]*>(\\d+)</td></tr>\\s*" +
                "<tr><td[^>]*>Covert\\s+Ops</td><td[^>]*>(\\d+)</td></tr>\\s*" +
                "<tr><td[^>]*>Asteroid\\s+Mining</td><td[^>]*>(\\d+)</td></tr>");
        regexMatcher = compiledRegex.matcher(getScanpage().replace(",", ""));
        regexMatcher.find();

        spaceTravel = new BigDecimal(regexMatcher.group(1));
        infraStructure = new BigDecimal(regexMatcher.group(2));
        hulls = new BigDecimal(regexMatcher.group(3));
        waves = new BigDecimal(regexMatcher.group(4));
        coreExtraction = new BigDecimal(regexMatcher.group(5));
        covertOps = new BigDecimal(regexMatcher.group(6));
        asteroidMining = new BigDecimal(regexMatcher.group(7));

        try {
            addDevelopmentAnalyse();
        } catch (AddDevelopmentException e) {
            return e.getError();
        }

        return "Development Scan on " + getX() + ":" + getY() + ":" + getZ() + " parsed successfuly";
    }

    public void addDevelopmentAnalyse() throws AddDevelopmentException {

        org.deschutter.hibernate.Development dev = new org.deschutter.hibernate.Development();
        dev.setScansByScanId(getHbscan());

        dev.setMrefinery(metalRefinery);
        dev.setCrefinery(crystalRefinery);
        dev.setErefinery(eoniumRefinery);

        dev.setLfactory(lightFactory);
        dev.setMfactory(mediumFactory);
        dev.setHfactory(heavyFactory);

        dev.setFcentre(financeCentre);
        dev.setScentre(securityCentre);
        dev.setWdist(distorter);
        dev.setWamp(amplifier);
        dev.setRlab(researchLab);

        dev.setStravel(spaceTravel);
        dev.setWaves(waves);
        dev.setInfrastructure(infraStructure);
        dev.setHulls(hulls);
        dev.setCoreExtracting(coreExtraction);
        dev.setCovert_ops(covertOps);
        dev.setAsteroidmining(asteroidMining);
        try {
            Transaction tx = getSession().beginTransaction();
            getSession().saveOrUpdate(dev);
            tx.commit();
        } catch (Exception e) {
            throw new AddDevelopmentException(e.getMessage());
        }

        //Queries.addDevelopmentAnalyse(getID(),getX(),getY(),getZ(),getTick(),lightFactory, mediumFactory, heavyFactory, amplifier, distorter, metalRefinery, crystalRefinery, eoniumRefinery, researchLab, financeCentre, securityCentre,spaceTravel, infraStructure, hulls, waves, coreExtraction, covertOps, asteroidMining);
    }

    public BigDecimal getTypeID() {
        return SCAN_TYPE_DVELOPMENT;
    }

    public BigDecimal getLightFactory() {
        return lightFactory;
    }

    public void setLightFactory(BigDecimal lightFactory) {
        this.lightFactory = lightFactory;
    }

    public BigDecimal getMediumFactory() {
        return mediumFactory;
    }

    public void setMediumFactory(BigDecimal mediumFactory) {
        this.mediumFactory = mediumFactory;
    }

    public BigDecimal getHeavyFactory() {
        return heavyFactory;
    }

    public void setHeavyFactory(BigDecimal heavyFactory) {
        this.heavyFactory = heavyFactory;
    }

    public BigDecimal getAmplifier() {
        return amplifier;
    }

    public void setAmplifier(BigDecimal amplifier) {
        this.amplifier = amplifier;
    }

    public BigDecimal getDistorter() {
        return distorter;
    }

    public void setDistorter(BigDecimal distorter) {
        this.distorter = distorter;
    }

    public BigDecimal getMetalRefinery() {
        return metalRefinery;
    }

    public void setMetalRefinery(BigDecimal metalRefinery) {
        this.metalRefinery = metalRefinery;
    }

    public BigDecimal getCrystalRefinery() {
        return crystalRefinery;
    }

    public void setCrystalRefinery(BigDecimal crystalRefinery) {
        this.crystalRefinery = crystalRefinery;
    }

    public BigDecimal getEoniumRefinery() {
        return eoniumRefinery;
    }

    public void setEoniumRefinery(BigDecimal eoniumRefinery) {
        this.eoniumRefinery = eoniumRefinery;
    }

    public BigDecimal getResearchLab() {
        return researchLab;
    }

    public void setResearchLab(BigDecimal researchLab) {
        this.researchLab = researchLab;
    }

    public BigDecimal getFinanceCentre() {
        return financeCentre;
    }

    public void setFinanceCentre(BigDecimal financeCentre) {
        this.financeCentre = financeCentre;
    }

    public BigDecimal getSecurityCentre() {
        return securityCentre;
    }

    public void setSecurityCentre(BigDecimal securityCentre) {
        this.securityCentre = securityCentre;
    }

    public BigDecimal getSpaceTravel() {
        return spaceTravel;
    }

    public void setSpaceTravel(BigDecimal spaceTravel) {
        this.spaceTravel = spaceTravel;
    }

    public BigDecimal getInfraStructure() {
        return infraStructure;
    }

    public void setInfraStructure(BigDecimal infraStructure) {
        this.infraStructure = infraStructure;
    }

    public BigDecimal getHulls() {
        return hulls;
    }

    public void setHulls(BigDecimal hulls) {
        this.hulls = hulls;
    }

    public BigDecimal getWaves() {
        return waves;
    }

    public void setWaves(BigDecimal waves) {
        this.waves = waves;
    }

    public BigDecimal getCoreExtraction() {
        return coreExtraction;
    }

    public void setCoreExtraction(BigDecimal coreExtraction) {
        this.coreExtraction = coreExtraction;
    }

    public BigDecimal getCovertOps() {
        return covertOps;
    }

    public void setCovertOps(BigDecimal covertOps) {
        this.covertOps = covertOps;
    }

    public BigDecimal getAsteroidMining() {
        return asteroidMining;
    }

    public void setAsteroidMining(BigDecimal asteroidMining) {
        this.asteroidMining = asteroidMining;
    }
}
